package com.yx.changdao.web.controller;

import com.yx.changdao.common.data.RespCodeEnum;
import com.yx.changdao.common.data.ResultCodeEnum;
import com.yx.changdao.common.entity.CommunityTownship;
import com.yx.changdao.common.entity.CommunityVillage;
import com.yx.changdao.common.entity.GridInfo;
import com.yx.changdao.common.vo.BuildBuildVo;
import com.yx.changdao.service.CommunityVillageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.SysConst;
import com.yx.changdao.common.utils.SysUtils;
import com.yx.changdao.service.GridInfoService;
import com.yx.changdao.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.mvc.annotation.GET;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: <br/>
 * 村/社区
 * <p>
 * <br/>
 * @Auther: walter
 * @create 2020/04/18 15:48
 */
@RestController
@RequestMapping("community/village")
public class CommunityVillageController {

    /**
     * 村/社区服务
     */
    @Autowired
    private CommunityVillageService communityVillageService;

    @Autowired
    private GridInfoService gridInfoService;

    /**
     * 列表
     *
     * @return Result
     */
    @AutoLog
    @GetMapping("/list")
    public Result list() throws AlertException {
        return Result.ok().data(communityVillageService.list(SysUtils.autoCnd(CommunityVillage.class)));
    }


    /**
     * 添加村/社区
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PostMapping
    public Result add(@RequestBody CommunityVillage communityVillage) throws AlertException {
        CommunityVillage communityVillage1 = communityVillageService
                .getOne(new QueryWrapper<CommunityVillage>()
                        .eq(CommunityVillage.VILLAGE_NAME, communityVillage.getVillageName())
                .eq(CommunityVillage.TOWN_SHIP_ID,communityVillage.getTownShipId()));
        if(communityVillage1!=null){
            throw new AlertException(communityVillage.getVillageName()+" 村/社区已存在");
        }
        boolean b = communityVillageService.save(communityVillage);
        if (!b) {
            throw new AlertException("村/社区添加失败");
        }
        CommunityVillage one = communityVillageService
                .getOne(new QueryWrapper<CommunityVillage>()
                        .eq(CommunityVillage.VILLAGE_NAME, communityVillage.getVillageName())
                        .eq(CommunityVillage.TOWN_SHIP_ID,communityVillage.getTownShipId()));
        BuildBuildVo buildBuildVo =new BuildBuildVo();
        buildBuildVo.setId(one.getId());
        buildBuildVo.setName(one.getVillageName());

        return Result.ok("村/社区添加成功").data(buildBuildVo);
    }


    /**
     * 获得指定id 村/社区。
     *
     * @param
     * @return Result
     */
    @AutoLog
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") String id) throws AlertException {
        CommunityVillage communityVillage = communityVillageService.getById(id);
        if (communityVillage == null) {
            throw new AlertException("未找到相关村/社区信息");
        }
        return Result.ok().data(communityVillage);
    }


    /**
     * 更新村/社区
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PutMapping
    public Result update(@RequestBody CommunityVillage communityVillage) throws AlertException {
        CommunityVillage one = communityVillageService.getOne(new QueryWrapper<CommunityVillage>().eq(CommunityVillage.ID, communityVillage.getId()));

        CommunityVillage list = communityVillageService.getOne(new QueryWrapper<CommunityVillage>()
                .eq(CommunityVillage.VILLAGE_NAME, communityVillage.getVillageName())
        .eq(CommunityVillage.TOWN_SHIP_ID,one.getTownShipId()));

        if(list!=null&&!list.getId().equals(communityVillage.getId())){
            throw new AlertException(communityVillage.getVillageName()+ " 村/社区已存在");
        }

        boolean b = communityVillageService.updateById(communityVillage);
        if (!b) {
            throw new AlertException("村/社区更新失败");
        }
        return Result.ok("村/社区更新成功");
    }


    /**
     * 删除村/社区
     *
     * @param
     * @return Result
     */
    @AutoLog
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) throws AlertException {
        CommunityVillage one = communityVillageService.getOne(new QueryWrapper<CommunityVillage>().eq(CommunityVillage.ID, id));
        BuildBuildVo buildBuildVo =new BuildBuildVo();
        buildBuildVo.setName(one.getVillageName());
        buildBuildVo.setId(one.getId());
       /* boolean b1 = communityVillageService.removeGrid(Integer.parseInt(id));
        if (!b1) {
            throw new AlertException("村/社区删除失败");
        }*/
       //看改村下是否有网格
        List<GridInfo> list = gridInfoService.list(new QueryWrapper<GridInfo>().eq(GridInfo.VILLAGE_ID, id));
        if(list.size()!=0){
            return new Result(RespCodeEnum.OK, null, ResultCodeEnum.DELETE, "您删除的层级内包含其他数据信息!", buildBuildVo);
        }

        boolean b = communityVillageService.removeById(id);
        if (!b) {
            throw new AlertException("村/社区删除失败");
        }

        return Result.ok("删除村/社区成功").data(buildBuildVo);
    }

    /**
     * 根据乡镇查id
     * @param townId
     * @return
     * @throws AlertException
     */
    @GetMapping("/getVillageByTownId")
    @AutoLog
    public  Result getVillageByTownId(@RequestParam("townId")Integer townId)throws AlertException{
        List<CommunityVillage> villageByTownId = communityVillageService.getVillageByTownId(townId);
        List<BuildBuildVo> buildBuildVoList=new ArrayList<>();
        for (CommunityVillage communityBuildDetail : villageByTownId) {
            BuildBuildVo buildBuildVo=new BuildBuildVo();
            buildBuildVo.setId(communityBuildDetail.getId());
            buildBuildVo.setName(communityBuildDetail.getVillageName());
            buildBuildVo.setParent_id(communityBuildDetail.getTownShipId());
            buildBuildVoList.add(buildBuildVo);
        }

        return Result.ok().data(buildBuildVoList);
    }

}
